package cn.com.checne.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import cn.com.checne.model.domain.Watermark;


@Repository
public class WatermarkDao {
	protected static final Logger log = Logger.getLogger(WatermarkDao.class);

	/**
	 * create
	 * @param cate
	 * @return
	 */
	public boolean create(Watermark wm) {
		boolean result = false;
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			String createSql = "insert into tbl_watermark(wm_lg_id_fk,wm_switchstatus,wm_font,wm_fontsize,wm_fontstyle,wm_toward," +
					"wm_fontcolor,wm_scale,wm_alpha,wm_mark)" + 
					" values(?,?,?,?,?,?,?,?,?,?)";
			PreparedStatement pstmt = conn.prepareStatement(createSql);
			int i = 0 ;
			pstmt.setInt(++i,wm.getLg());
			pstmt.setInt(++i,wm.getSwitchStatus());
			pstmt.setString(++i,wm.getFont());
			pstmt.setInt(++i,wm.getFontSize());
			pstmt.setInt(++i,wm.getFontStyle());
			pstmt.setInt(++i,wm.getToward());
			pstmt.setInt(++i,wm.getFontColor());
			pstmt.setInt(++i,wm.getScale());
			pstmt.setFloat(++i,wm.getAlpha());
			pstmt.setString(++i,wm.getMark());

			pstmt.execute();
			conn.close();
			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false ;
		}
		return result;
	}

	/**
	 * @param cate
	 * @return
	 */
	public boolean update(Watermark wm) {
		boolean result = false;
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			String updateSql = "update tbl_watermark set " +
					"wm_switchstatus = ?,wm_font = ?,wm_fontsize = ?,wm_fontstyle = ?,wm_toward =? ,wm_fontcolor = ?,wm_scale =?,wm_alpha=?," +
					"wm_mark =? where wm_lg_id_fk = ? ";
			PreparedStatement pstmt = conn.prepareStatement(updateSql);
			int i = 0 ;
			pstmt.setInt(++i,wm.getSwitchStatus());
			pstmt.setString(++i,wm.getFont());
			pstmt.setInt(++i,wm.getFontSize());
			pstmt.setInt(++i,wm.getFontStyle());
			pstmt.setInt(++i,wm.getToward());
			pstmt.setInt(++i,wm.getFontColor());
			pstmt.setInt(++i,wm.getScale());
			pstmt.setFloat(++i,wm.getAlpha());
			pstmt.setString(++i,wm.getMark());
			pstmt.setInt(++i,wm.getLg());
			
			pstmt.executeUpdate();
			conn.close();
			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false ;
		}
		return result;
	}

	/**
	 * 
	 * @param wmId
	 * @return
	 */
	public Watermark getWatermarkByLgId(int lgId) {
		Watermark wm = new Watermark();
		try {
			lgId = lgId == 0 ? 1 : lgId ;
			Connection conn = DBFactory.getInstance().getConnection();
			String sql = "select * from tbl_watermark where wm_lg_id_fk = ? limit 0,1 ";

			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, lgId);
			log.info("[SQL]" + pstmt.toString());
			ResultSet rs = pstmt.executeQuery();
			List<Watermark> list = getResult(rs);
			wm = (list != null && list.size() > 0 ) ? list.get(0) : wm ;
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return wm;
	}
	/**
	 * get rseult from resultSet
	 * @param rs
	 * @return
	 */
	public List<Watermark> getResult(ResultSet rs) {
		List<Watermark> list = new ArrayList<Watermark>();
		try {
			
//			String createSql = "insert into tbl_watermark(wm_lg_id_fk,,wm_font,,,," +
//			",,,)" + 
			Watermark wm = null ;
			while(rs.next()) {
				wm = new Watermark();
				wm.setLg(rs.getInt("wm_lg_id_fk"));
				wm.setSwitchStatus(rs.getInt("wm_switchstatus"));
				wm.setFont(rs.getString("wm_font"));
				wm.setFontSize(rs.getInt("wm_fontsize"));
				wm.setFontStyle(rs.getInt("wm_fontstyle"));
				wm.setToward(rs.getInt("wm_toward"));
				wm.setFontColor(rs.getInt("wm_fontcolor"));
				wm.setScale(rs.getInt("wm_scale"));
				wm.setAlpha(rs.getFloat("wm_alpha"));
				wm.setMark(rs.getString("wm_mark"));
				list.add(wm);
			}
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	public boolean isWaterOpen(int languageId){
		Watermark mark = getWatermarkByLgId(languageId);
		return mark != null && mark.getSwitchStatus() == 1 ;
	}

	
}